package com.example.curriculum;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

//应用Volley框架解析JSON数据。
public class WeatherActivity extends AppCompatActivity {
    TextView CityName ,tianqi, max , min;
    ImageView img;
    //String url="http://127.0.0.1/test/jsonData.json";
    String url = "http://www.weather.com.cn/data/cityinfo/101010100.html";
    // http://t.weather.sojson.com/api/weather/city/101010100
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        CityName = (TextView)findViewById(R.id.CityName);//城市名
        tianqi = (TextView)findViewById(R.id.tianqi);//天气
        max = (TextView)findViewById(R.id.max);//最高温
        min = (TextView)findViewById(R.id.min);//最低温
        img = (ImageView)findViewById(R.id.img);


        getData();
    }


    public void getData() {
        //1 排队
        RequestQueue mq = Volley.newRequestQueue(this);
        //2 轮到提要求
        JsonObjectRequest stringRequest= new JsonObjectRequest(
                url,
                null,
                new Response.Listener<JSONObject>(){
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            //        {"weatherinfo":{"city":"北京","cityid":"101010100",
                            //         "temp1":"18℃","temp2":"31℃","weather":"多云转阴",
                            //         "img1":"d1.gif","img2":"n1.gif","ptime":"08:00"}}
                            JSONObject mjson=response.getJSONObject("weatherinfo");
                            String city=new String(mjson.getString("city"));
                            String cityid=new String(mjson.getString("cityid"));
                            String temp1=new String(mjson.getString("temp1"));
                            String temp2=new String(mjson.getString("temp2"));
                            String weather=new String(mjson.getString("weather"));
                            String img1=new String(mjson.getString("img1"));
                            String img2=new String(mjson.getString("img2"));
                            String ptime=new String(mjson.getString("ptime"));
                            String s="[city=" + city + ", cityid=" + cityid + ", temp1="
                                    + temp1 + ", temp2=" + temp2 + ", weather=" + weather
                                    + ", ptime=" + ptime + "]";
                            CityName.setText(city);
                            tianqi.setText(weather);
                            if(weather.equals("多云转阴")){
                                img.setBackgroundResource(R.drawable.a9);
                            }
                            max.setText(temp2);
                            min.setText(temp1);
                            Log.d("TAG", s);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener(){
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        CityName.setText("网页访问失败");
                    }
                }){  //将volley默认的ISO-8859-1格式转换为utf-8格式
            @Override
            protected Response<JSONObject> parseNetworkResponse(
                    NetworkResponse response) {
                try {  //jsonObject要和前面的类型一致,此处都是JSONObject
                    JSONObject jsonObject = new JSONObject(
                            new String(response.data, "UTF-8"));
                    return Response.success(jsonObject,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je)) ;
                }
            }
        };
        //3 结束服务，把数据带上
        mq.add(stringRequest);
    }

}

